package com.welinkcredit.util;

import java.io.File;
import java.util.Date;

import com.aliyun.oss.OSSClient;

public class OSSUploadUtil {

	// 图片上传OSS
	@SuppressWarnings("unused")
	public String uploadPicOSS(String id, String prefix,String fileName, File file) {
		// 图片上传
		String Objectkey = prefix +id+ "/" + CommonUtil.getRandNum(3)+new Date().getTime()/1000 + getExtention(fileName);
		//System.out.println("======================>>Objectkey>"+Objectkey);
		// 使用默认的OSS服务器地址创建OSSClient对象,不叫OSS_ENDPOINT代表使用杭州节点，青岛节点要加上不然包异常
		OSSClient client = new OSSClient(OSSUtil.OSS_ENDPOINT,OSSUtil.ACCESS_ID, OSSUtil.ACCESS_KEY);
		try {
			OSSUtil.uploadFile(client, OSSUtil.BUCKET_NAME, Objectkey,fileName, file);
			//System.out.println("===================>>>uploadSuccess");
		} catch (Exception e) {
			Objectkey = "";
			System.out.println("=================>>>fail");
			e.printStackTrace();
		}
		return Objectkey;
	}
	/**
	 * 
	 * @param fileName
	 * @return
	 */
	// 获取后缀
	private static String getExtention(String fileName) {
		int pos = 0;
		if (fileName != null) {
			pos = fileName.lastIndexOf(".");
			System.out.println("====pos:"+pos);
			if(pos < 0){
				return "";
			}
			return fileName.substring(pos);
		} else {
			return "";
		}
	}
}
